package com.sunnada.wfs;

import com.sunnada.wfs.geoserverRest.GeoServerRest;
import com.sunnada.wfs.geoserverRest.GerServerService;
import com.sunnada.wfs.service.FileService;
import com.sunnada.wfs.service.GeometryColumnsService;
import com.sunnada.wfs.service.SystemInfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@RestController("wfs")
//@RequestMapping("wfs")
public class Controller {

    @Autowired
    private GerServerService gerServerService;

    @Autowired
    private GeometryColumnsService geometryColumnsService;

    @Autowired
    private FileService fileService;

    @Autowired
    private SystemInfoService systemInfoService;

    @ApiOperation(value="保存文件", notes="保存文件",httpMethod = "POST")
    @RequestMapping(value = "/upload")
    @ResponseBody
    public String saveFile(@RequestParam("file") MultipartFile file) throws Exception{
        return fileService.saveFile(file);
    }

    @ApiOperation(value="部署资源",httpMethod = "GET")
    @ApiImplicitParam(name = "featureTypeName",required = true,dataType = "String",paramType = "query")
    @ResponseBody
    @GetMapping("/deploy")
    public String deploy(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception{
        String featureTypeName = httpRequest.getParameter("featureTypeName");
        if (gerServerService.deploy("featureTypeName")) return "发布成功";
        return "发布失败";
    }

    @ApiOperation(value="获取数据库中已上传的资源",httpMethod = "GET")
    @ResponseBody
    @GetMapping("/getFeatureTypes")
    public String getFeatureTypes(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception{
        return geometryColumnsService.getGeoTable().toString();
    }

    @ApiOperation(value="上传系统信息",httpMethod = "POST")
    @ApiImplicitParam(name = "systemInfo",required = true,dataType = "String",paramType = "query")
    @ResponseBody
    @PostMapping("/systemInfo")
    public String systemInfo(HttpServletRequest request, HttpServletResponse response){
        String json = request.getParameter("systemInfo");
        if (StringUtils.isNotBlank(json)){
            systemInfoService.update(getIpAddr(request),json);
        }
        return "上传成功";
    }

    @ApiOperation(value="获取系统信息",httpMethod = "GET")
    @ApiImplicitParam(name = "ip",required = true,dataType = "String",paramType = "path")
    @ResponseBody
    @GetMapping("/systemInfo/{ip}")
    public String getSystemInfo(@PathVariable("ip") String ip){
        if (StringUtils.isNotBlank(ip)){
            return systemInfoService.get(ip).toString();
        }
        return "ip为空";
    }

    @ApiOperation(value="获取系统信息",httpMethod = "GET")
    @ResponseBody
    @GetMapping("/systemInfo")
    public String getSystemInfoAll(){
        return systemInfoService.getAll().toString();
    }



    private Map<String,String> toMapStringString(Map<String, String[]> params){
        Map<String,String> result = new HashMap<>();
        for (String key : params.keySet()) {
            if (params.get(key).length!=0) {
                String value = params.get(key)[0];
                result.put(key, value);
            }
        }
        return result;
    }

    // 返回用IP地址
    private String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader(" x-forwarded-for ");
        if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
            ip = request.getHeader(" Proxy-Client-IP ");
        }
        if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
            ip = request.getHeader(" WL-Proxy-Client-IP ");
        }
        if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}